﻿using System;

public class ToRGB {
	static internal float HueToRGB( float rm1, float rm2, float rh ) {
		if ( rh > 360.0f )
			rh -= 360.0f;
		else if ( rh < 0.0f )
			rh += 360.0f;

		if ( rh < 60.0f )
			rm1 = rm1 + ( rm2 - rm1 ) * rh / 60.0f;
		else if ( rh < 180.0f )
			rm1 = rm2;
		else if ( rh < 240.0f )
			rm1 = rm1 + ( rm2 - rm1 ) * ( 240.0f - rh ) / 60.0f;

		return rm1;
	}

	static internal void HLStoRGB_ONE( float H, float L, float S, out byte R, out byte G, out byte B ) {
		float tempr, tempg, tempb;
		float m1, m2;

		if ( S == 0 ) {
			tempr = tempg = tempb = L;
		}
		else {
			if ( L <= 0.5 )
				m2 = L * ( 1.0f + S );
			else if ( L == 1.0f )
				m2 = L;
			else
				m2 = L + S - L * S;
			m1 = 2.0f * L - m2;
			tempr = HueToRGB( m1, m2, H + 1.0f / 3.0f );
			tempg = HueToRGB( m1, m2, H );
			tempb = HueToRGB( m1, m2, H - 1.0f / 3.0f );
		}
		R = ( byte )( tempr * 255f );
		G = ( byte )( tempg * 255f );
		B = ( byte )( tempb * 255f );
	}
}
